package com.my.zidingyikongjian.view.six;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * 项目名称：画手指路径
 * 创建人：mwb
 * 创建时间：2018/7/23 9:45
 */

public class DrawFingerTrackPath extends View {

    private Path mPath = new Path();
    private float mPreX, mPreY;

    public DrawFingerTrackPath(Context context) {
        super(context);
    }

    public DrawFingerTrackPath(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN: {// 按下屏幕
                mPath.moveTo(event.getX(), event.getY());
                mPreX = event.getX();
                mPreY = event.getY();
                return true;
            }
            case MotionEvent.ACTION_MOVE: // 移动手势
                float endX = (mPreX + event.getX()) / 2;
                float endY = (mPreY + event.getY()) / 2;
//                mPath.lineTo(event.getX(), event.getY());
                mPath.quadTo(mPreX, mPreY, endX, endY);
                mPreX = event.getX();
                mPreY = event.getY();
                postInvalidate();
                break;
            default:
                break;
        }

        return super.onTouchEvent(event);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setColor(Color.GREEN);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(2);

        canvas.drawPath(mPath, paint);
    }

    /**
     * 重置
     */
    public void reset() {
        mPath.reset();
        invalidate(); // 请屏，重置画布,调用之后才能重新绘制画布
    }
}
